home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / gdb-4.12 / sim / z8k / writecod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  36.5 KB  |  1,929 lines

  1. /* generate instructions for Z8KSIM
  2.    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of Z8KSIM
  5.  
  6. Z8KSIM is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Z8KSIM is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Z8KSIM; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* This program generates the code which emulates each of the z8k
  21.    instructions
  22.  
  23.    code goes into three files, tc-gen1.h, tc-gen2.h and tc-gen3.h.
  24.    which file being made depends upon the options
  25.  
  26.    -1  tc-gen1.h contains the fully expanded code for some selected
  27.        opcodes, (those in the quick.c list)
  28.  
  29.    -2   tc-gen2.h contains a list of pointers to functions, one for each
  30.    opcode.  It points to functions in tc-gen3.h and tc-gen1.h
  31.    depending upon quick.c
  32.  
  33.    -3   tc-gen3.h contains all the opcodes in unexpanded form.
  34.  
  35.    -b3   tc-genb3.h same as -3 but for long pointers
  36.  
  37.    -m  regenerates list.c, which is an inverted list of opcodes to
  38.        pointers into the z8k dissassemble opcode table, it's just there
  39.        to makes things faster.
  40.    */
  41.  
  42. /* steve chamberlain
  43.    sac@cygnus.com */
  44.  
  45. #include <ansidecl.h>
  46. #include "sysdep.h"
  47.  
  48. #define NICENAMES
  49.  
  50. #define DEFINE_TABLE
  51. #include "../opcodes/z8k-opc.h"
  52.  
  53. #define NOPS 500
  54.  
  55. extern short z8k_inv_list[];
  56. struct opcode_value
  57. {
  58.   int n;
  59.   struct opcode_value *next;
  60. };
  61.  
  62. #define NICENAMES
  63. int BIG;
  64.  
  65. static char *reg_names[] =
  66. {"bad", "src", "dst", "aux_a", "aux_b", "aux_r", "aux_x"};
  67.  
  68. #define IS_DST(x) ((x & 0xf) == 2)
  69. #define IS_SRC(x) ((x & 0xf)==1)
  70. #define SIZE_ADDRESS (BIG ? 8 : 4)    /* number of nibbles in a ptr*/
  71.  
  72. static int file;
  73. static int makelist;
  74.  
  75. static int nibs = 0;
  76.  
  77. static char *current_size;
  78. static char *current_name;
  79. static char current_word0[40];
  80. static char current_byte0[40];
  81. static char current_byte1[40];
  82. static int indent;
  83. static char *p;
  84. static char *d;
  85.  
  86. struct opcode_value *list[NOPS];
  87.  
  88. static opcode_entry_type *
  89. lookup_inst (what)
  90.      int what;
  91. {
  92.   if (makelist)
  93.     {
  94.  
  95.       int nibl_index;
  96.       int nibl_matched;
  97.       unsigned short instr_nibl;
  98.       unsigned short tabl_datum, datum_class, datum_value;
  99.       char instr_nibbles[8];
  100.  
  101.       opcode_entry_type *ptr = z8k_table;
  102.  
  103.       nibl_matched = 0;
  104.  
  105.       instr_nibbles[3] = (what >> 0) & 0xf;
  106.       instr_nibbles[2] = (what >> 4) & 0xf;
  107.       instr_nibbles[1] = (what >> 8) & 0xf;
  108.       instr_nibbles[0] = (what >> 12) & 0xf;
  109.  
  110.       while (ptr->name)
  111.     {
  112.       nibl_matched = 1;
  113.       for (nibl_index = 0; nibl_index < 4 && nibl_matched; nibl_index++)
  114.         {
  115.           instr_nibl = instr_nibbles[nibl_index];
  116.  
  117.           tabl_datum = ptr->byte_info[nibl_index];
  118.           datum_class = tabl_datum & CLASS_MASK;
  119.           datum_value = ~CLASS_MASK & tabl_datum;
  120.  
  121.           switch (datum_class)
  122.         {
  123.         case CLASS_BIT_1OR2:
  124.           if (datum_value != (instr_nibl & ~0x2))
  125.             nibl_matched = 0;
  126.           break;
  127.  
  128.         case CLASS_BIT:
  129.           if (datum_value != instr_nibl)
  130.             nibl_matched = 0;
  131.           break;
  132.         case CLASS_00II:
  133.           if (!((~instr_nibl) & 0x4))
  134.             nibl_matched = 0;
  135.           break;
  136.         case CLASS_01II:
  137.           if (!(instr_nibl & 0x4))
  138.             nibl_matched = 0;
  139.           break;
  140.         case CLASS_0CCC:
  141.           if (!((~instr_nibl) & 0x8))
  142.             nibl_matched = 0;
  143.           break;
  144.         case CLASS_1CCC:
  145.           if (!(instr_nibl & 0x8))
  146.             nibl_matched = 0;
  147.           break;
  148.         case CLASS_0DISP7:
  149.           if (!((~instr_nibl) & 0x8))
  150.             nibl_matched = 0;
  151.           nibl_index += 1;
  152.           break;
  153.         case CLASS_1DISP7:
  154.           if (!(instr_nibl & 0x8))
  155.             nibl_matched = 0;
  156.           nibl_index += 1;
  157.           break;
  158.         case CLASS_REGN0:
  159.           if (instr_nibl == 0)
  160.             nibl_matched = 0;
  161.           break;
  162.         default:
  163.           break;
  164.         }
  165.         }
  166.       if (nibl_matched)
  167.         {
  168.           return ptr;
  169.         }
  170.       ptr++;
  171.     }
  172.       return 0;
  173.     }
  174.   else
  175.     {
  176.  
  177.       if (z8k_inv_list[what] < 0)
  178.     return 0;
  179.       return z8k_table + z8k_inv_list[what];
  180.     }
  181. }
  182.  
  183. static char *
  184. insn_4 (n)
  185.      int n;
  186. {
  187.   switch (n)
  188.     {
  189.     case 1:
  190.       return "((iwords_0>>8) & 0xf)";
  191.     case 2:
  192.       return "((ibytes_1 >> 4) & 0xf)";
  193.     case 3:
  194.       return "((ibytes_1) & 0xf)";
  195.     case 4:
  196.       return "((ibytes_2>>4) & 0xf)";
  197.     case 5:
  198.       return "((ibytes_2) & 0xf)";
  199.     case 6:
  200.       return "((ibytes_3 >> 4) & 0xf)";
  201.     case 7:
  202.       return "((ibytes_3) & 0xf)";
  203.     default:
  204.       return "****";
  205.     }
  206. }
  207.  
  208. char *
  209. ptr_mode ()
  210. {
  211.   if (BIG)
  212.     {
  213.       return "ptr_long";
  214.     }
  215.   return "word";
  216. }
  217.  
  218. static
  219. char *
  220. ptr_size ()
  221. {
  222.   if (BIG)
  223.     return "4";
  224.   return "2";
  225. }
  226.  
  227. static char *
  228. reg_n (x)
  229.      unsigned int x;
  230. {
  231.   return reg_names[x & 0xf];
  232. }
  233.  
  234. char *
  235. stack_ptr ()
  236. {
  237.   return BIG ? "14" : "15";
  238. }
  239.  
  240. char *
  241. mem ()
  242. {
  243. #if 0
  244.   return BIG ? "segmem" : "unsegmem";
  245. #else
  246.   return "mem";
  247. #endif
  248. }
  249.  
  250. int
  251. match (a)
  252.      char *a;
  253. {
  254.   if (strncmp (p, a, strlen (a)) == 0)
  255.     {
  256.       p += strlen (a);
  257.       return 1;
  258.     }
  259.   return 0;
  260. }
  261.  
  262. static
  263. void
  264. sub (y)
  265.      char *y;
  266. {
  267.   sprintf (d, "%s", y);
  268.   d += strlen (d);
  269. }
  270.  
  271. static char *
  272. insn_16 (n)
  273.      int n;
  274. {
  275.   switch (n)
  276.     {
  277.     case 0:
  278.       return "(iwords_0)";
  279.     case 4:
  280.       return "(iwords_1)";
  281.     case 8:
  282.       return "(iwords_2)";
  283.     case 12:
  284.       return "(iwords_3)";
  285.     default:
  286.       return "****";
  287.     }
  288. }
  289.  
  290. static
  291. char *
  292. insn_32 (n)
  293.      int n;
  294. {
  295.   switch (n)
  296.     {
  297.     case 0:
  298.       return "((iwords_0<<16) | (iwords_1))";
  299.     case 4:
  300.       return "((iwords_1<<16) | (iwords_2))";
  301.     case 8:
  302.       return "((iwords_2<<16) | (iwords_3))";
  303.     default:
  304.       return "****";
  305.     }
  306. }
  307.  
  308. static char *
  309. size_name (x)
  310.      int x;
  311. {
  312.   switch (x)
  313.     {
  314.     case 8:
  315.       return "byte";
  316.     case 16:
  317.       return "word";
  318.     case 32:
  319.       return "long";
  320.     case 64:
  321.       return "quad";
  322.     }
  323.   return "!!";
  324. }
  325.  
  326. /*VARARGS*/
  327. void
  328. emit (string, a1, a2, a3, a4, a5)
  329.      char *string;
  330.      char* a1;
  331.      char* a2;
  332.      char* a3;
  333.      char* a4;
  334.      char* a5;
  335. {
  336.   int indent_inc = 0;
  337.   int indent_dec = 0;
  338.   int i;
  339.   char buffer[1000];
  340.  
  341.   d = buffer;
  342.   p = string;
  343.  
  344.   while (*p)
  345.     {
  346.       if (match ("<fop>"))
  347.     {
  348.       if (BIG)
  349.         {
  350.           sub ("bfop");
  351.         }
  352.       else
  353.         {
  354.           sub ("sfop");
  355.         }
  356.     }
  357.       else if (match ("<iptr>"))
  358.     {
  359.       if (BIG)
  360.         {
  361.           switch (nibs)
  362.         {
  363.         case 4:
  364.           sub ("(((iwords_1 << 8) | (iwords_2)) & 0x7fffff)");
  365.           break;
  366.         default:
  367.           sub ("fail(context,124)");
  368.           break;
  369.         }
  370.         }
  371.       else
  372.         {
  373.           switch (nibs)
  374.         {
  375.         case 4:
  376.           sub ("iwords_1");
  377.           break;
  378.         default:
  379.           sub ("fail(context,123)");
  380.           break;
  381.         }
  382.         }
  383.     }
  384.       else if (match ("<name>"))
  385.     {
  386.       sub (current_name);
  387.     }
  388.       else if (match ("<size>"))
  389.     {
  390.       sub (current_size);
  391.     }
  392.       else if (match ("<insn_4>"))
  393.     {
  394.       sub (insn_4 (nibs));
  395.     }
  396.       else if (match ("<insn_16>"))
  397.     {
  398.       sub (insn_16 (nibs));
  399.     }
  400.       else if (match ("<insn_32>"))
  401.     {
  402.       sub (insn_32 (nibs));
  403.     }
  404.       else if (match ("iwords_0"))
  405.     {
  406.       sub (current_word0);
  407.     }
  408.       else if (match ("ibytes_0"))
  409.     {
  410.       sub (current_byte0);
  411.     }
  412.       else if (match ("<ibytes_1>"))
  413.     {
  414.       sub (current_byte1);
  415.     }
  416.       else if (match ("<next_size>"))
  417.     {
  418.       if (strcmp (current_size, "word") == 0)
  419.         sub ("long");
  420.       if (strcmp (current_size, "byte") == 0)
  421.         sub ("word");
  422.       else if (strcmp (current_size, "long") == 0)
  423.         sub ("quad");
  424.       else
  425.         abort ();
  426.     }
  427.       else if (match ("<addr_type>"))
  428.     {
  429.       if (BIG)
  430.         sub ("unsigned long");
  431.       else
  432.         sub ("unsigned short");
  433.     }
  434.  
  435.       else if (match ("<c_size>"))
  436.     {
  437.       if (strcmp (current_size, "word") == 0)
  438.         sub ("short");
  439.       else if (strcmp (current_size, "byte") == 0)
  440.         sub ("char");
  441.       else if (strcmp (current_size, "long") == 0)
  442.         sub ("long");
  443.       else
  444.         abort ();
  445.     }
  446.  
  447.       else if (match ("<pc>"))
  448.     {
  449.       sub ("pc");
  450.     }
  451.       else if (match ("<mem>"))
  452.     {
  453.       sub (mem ());
  454.     }
  455.       else if (match ("<sp>"))
  456.     {
  457.       sub (stack_ptr ());
  458.     }
  459.       else if (match ("<ptr_size>"))
  460.     {
  461.       sub (ptr_size ());
  462.     }
  463.       else if (match ("<ptr_mode>"))
  464.     {
  465.       sub (ptr_mode ());
  466.     }
  467.       else if (match ("<insn_8>"))
  468.     {
  469.       switch (nibs)
  470.         {
  471.         case 2:
  472.           sub ("(iwords_0&0xff)");
  473.           break;
  474.         case 4:
  475.           sub ("(iwords_1>>8)");
  476.           break;
  477.         case 6:
  478.           sub ("(iwords_1&0xff)");
  479.           break;
  480.         case 8:
  481.           sub ("(iwords_2>>8)");
  482.           break;
  483.         case 12:
  484.           sub ("(/* WHO */iwords_3&0xff)");
  485.           break;
  486.         default:
  487.           abort ();
  488.         }
  489.     }
  490.       else
  491.     {
  492.       if (*p == '{')
  493.         indent_inc++;
  494.       if (*p == '}')
  495.         indent_dec++;
  496.       *d++ = *p;
  497.       p++;
  498.     }
  499.     }
  500.   *d++ = 0;
  501.   indent -= indent_dec;
  502.   for (i = 0; i < indent; i++)
  503.     printf ("\t");
  504.   indent += indent_inc;
  505.   printf (buffer, a1, a2, a3, a4, a5);
  506.  
  507. }
  508.  
  509. /* fetch the lvalues of the operands */
  510. void
  511. info_args (p)
  512.      opcode_entry_type *p;
  513. {
  514.   unsigned int *s;
  515.  
  516.   int done_one_imm8 = 0;
  517.  
  518.   /* int done_read = 4;*/
  519.   s = p->byte_info;
  520.   nibs = 0;
  521.   while (*s)
  522.     {
  523.       switch (*s & CLASS_MASK)
  524.     {
  525.     case CLASS_BIT_1OR2:
  526.       emit ("register unsigned int imm_src=(<insn_4>& 2)?2:1;\n");
  527.       break;
  528.     case CLASS_BIT:
  529.       /* Just ignore these, we've already decoded this bit */
  530.       nibs++;
  531.       break;
  532.     case CLASS_REGN0:
  533.     case CLASS_REG:
  534.       /* this nibble tells us which register to use as an arg,
  535.          if we've already gobbled the nibble we know what to use */
  536.       {
  537.         int regname = *s & 0xf;
  538.  
  539.         emit ("register unsigned int reg_%s=<insn_4>;\n",
  540.           reg_names[regname]);
  541.  
  542.         nibs++;
  543.       }
  544.       break;
  545.     case CLASS_ADDRESS:
  546.       emit ("register unsigned base_%s=<iptr>;\n", reg_n (*s));
  547.  
  548.       nibs += SIZE_ADDRESS;
  549.  
  550.       break;
  551.     case CLASS_01II:
  552.     case CLASS_00II:
  553.       emit ("register unsigned int imm_src=<insn_4>&0x2;\n");
  554.       nibs++;
  555.       break;
  556.     case CLASS_IMM:
  557.       /* Work out the size of the think to fetch */
  558.  
  559.       {
  560.         switch (*s & ~CLASS_MASK)
  561.           {
  562.           case ARG_IMM16:
  563.         emit ("register unsigned imm_src=<insn_16>;\n");
  564.         nibs += 4;
  565.         break;
  566.           case ARG_IMM32:
  567.         emit ("register unsigned int imm_src= %s;\n", insn_32 (nibs));
  568.         nibs += 8;
  569.         break;
  570.           case ARG_IMM4:
  571.         emit ("register unsigned int imm_src=<insn_4>;\n");
  572.         nibs++;
  573.         break;
  574.           case ARG_IMM2:
  575.         emit ("register unsigned int imm_src=<insn_4> & 0x2;\n");
  576.         nibs++;
  577.         break;
  578.  
  579.           case ARG_IMM4M1:
  580.         emit ("register unsigned int imm_src=(<insn_4> + 1);\n");
  581.         nibs++;
  582.         break;
  583.           case ARG_IMM_1:
  584.         emit ("register unsigned int imm_src=1;\n");
  585.         break;
  586.           case ARG_IMM_2:
  587.         emit ("register unsigned int imm_src=2;\n");
  588.         break;
  589.           case ARG_NIM8:
  590.         emit ("register unsigned int imm_src=-<insn_8>;\n");
  591.         nibs += 2;
  592.         break;
  593.           case ARG_IMM8:
  594.         if (!done_one_imm8)
  595.           {
  596.             emit ("register unsigned int imm_src=<insn_8>;\n");
  597.             nibs += 2;
  598.             done_one_imm8 = 1;
  599.           }
  600.         break;
  601.           default:
  602.         emit ("register int fail%d=fail(context,1);\n", nibs);
  603.         break;
  604.           }
  605.         break;
  606.  
  607.     case CLASS_DISP8:
  608.         emit ("register unsigned int oplval_dst=(((char)ibytes_1)<<1) + pc;\n");
  609.         nibs += 2;
  610.         break;
  611.     case CLASS_CC:
  612.         emit ("register unsigned int op_cc=<insn_4>;\n");
  613.         nibs++;
  614.         break;
  615.     default:
  616.         emit ("register int FAIL%d=fail(context,2);\n", nibs);
  617.         break;
  618.       }
  619.       ;
  620.       /* work out how to fetch the immediate value */
  621.     }
  622.  
  623.       s++;
  624.     }
  625. }
  626.  
  627. void
  628. info_special (p, getdst, nostore, before, nosrc)
  629.      opcode_entry_type *p;
  630.      int *getdst;
  631.      int *nostore;
  632.      int *before;
  633.      int *nosrc;
  634. {
  635.   switch (p->opcode)
  636.     {
  637.     case OPC_exts:
  638.     case OPC_extsb:
  639.     case OPC_extsl:
  640.       *nostore = 1;
  641.       *nosrc = 1;
  642.       break;
  643.     case OPC_ldm:
  644.       *nostore = 1;
  645.       *nosrc = 1;
  646.       break;
  647.     case OPC_negb:
  648.     case OPC_neg:
  649.     case OPC_sla:
  650.     case OPC_slab:
  651.     case OPC_slal:
  652.     case OPC_sda:
  653.     case OPC_sdab:
  654.     case OPC_sdal:
  655.     case OPC_com:
  656.     case OPC_comb:
  657.     case OPC_adc:
  658.     case OPC_nop:
  659.     case OPC_adcb:
  660.     case OPC_add:
  661.     case OPC_addb:
  662.     case OPC_addl:
  663.     case OPC_inc:
  664.     case OPC_sub:
  665.     case OPC_subb:
  666.     case OPC_subl:
  667.     case OPC_and:
  668.     case OPC_andb:
  669.     case OPC_xorb:
  670.     case OPC_xor:
  671.       break;
  672.  
  673.     case OPC_mult:
  674.     case OPC_multl:
  675.     case OPC_div:
  676.     case OPC_divl:
  677.  
  678.       *nostore = 1;
  679.       break;
  680.  
  681.     case OPC_testb:
  682.     case OPC_test:
  683.     case OPC_testl:
  684.     case OPC_cp:
  685.     case OPC_cpb:
  686.     case OPC_cpl:
  687.     case OPC_bit:
  688.       *nostore = 1;
  689.       *before = 0;
  690.       break;
  691.  
  692.     case OPC_bpt:
  693.     case OPC_jr:
  694.     case OPC_jp:
  695.     case OPC_ret:
  696.     case OPC_call:
  697.     case OPC_tcc:
  698.       *nosrc = 1;
  699.       *nostore = 1;
  700.       *before = 1;
  701.       break;
  702.     case OPC_sc:
  703.       *nostore = 1;
  704.       *before = 0;
  705.       break;
  706.     case OPC_clrb:
  707.     case OPC_clr:
  708.       *before = 1;
  709.       *nosrc = 1;
  710.       break;
  711.     case OPC_ldi:
  712.     case OPC_ldib:
  713.     case OPC_lddb:
  714.     case OPC_ldd:
  715.  
  716.       *before = 1;
  717.       *nostore = 1;
  718.       *nosrc = 1;
  719.       break;
  720.     case OPC_ldk:
  721.     case OPC_ld:
  722.     case OPC_ldb:
  723.     case OPC_ldl:
  724.       *before = 1;
  725.       *getdst = 0;
  726.       break;
  727.     case OPC_push:
  728.     case OPC_pushl:
  729.     case OPC_pop:
  730.     case OPC_popl:
  731.       *before = 1;
  732.       *getdst = 0;
  733.       break;
  734.     case OPC_lda:
  735.       *nosrc = 1;
  736.       break;
  737.     }
  738. }
  739.  
  740. /* calculate the lvalues required for the opcode */
  741. void
  742. info_lvals (p)
  743.      opcode_entry_type *p;
  744. {
  745.   /* emit code to work out lvalues, if any */
  746.   unsigned int *i = p->arg_info;
  747.  
  748.   while (*i)
  749.     {
  750.       current_name = reg_n (*i);
  751.       current_size = size_name (p->type);
  752.       switch (*i & CLASS_MASK)
  753.     {
  754.     case CLASS_X:
  755.       /* address(reg) */
  756.       emit ("register  <addr_type> oplval_<name>= base_<name> + get_word_reg(context,reg_<name>);\n");
  757.       break;
  758.     case CLASS_IR:
  759.       /* Indirect register */
  760.       emit ("register  int oplval_<name> = get_<ptr_mode>_reg(context,reg_<name>);\n");
  761.       break;
  762.     case CLASS_DA:
  763.       emit ("register  int oplval_<name>=base_<name>;\n");
  764.       break;
  765.     case CLASS_IMM:
  766.     case CLASS_REG_WORD:
  767.     case CLASS_REG_LONG:
  768.     case CLASS_REG_BYTE:
  769.     case CLASS_PR:
  770.       break;
  771.     case CLASS_BA:
  772.       emit ("register  int oplval_<name> = get_<ptr_mode>_reg(context,reg_<name>) + (short)(imm_src);\n");
  773.       break;
  774.     case CLASS_BX:
  775.       emit ("register  int oplval_<name> = get_<ptr_mode>_reg(context,reg_<name>)\n");
  776.       emit ("  + get_word_reg(context,reg_aux_x);\n");
  777.       break;
  778.     }
  779.       i++;
  780.     }
  781. }
  782.  
  783. /* emit code to fetch the args from calculated lvalues */
  784. int allregs;
  785. void
  786. info_fetch (p, getdst)
  787.      opcode_entry_type *p;
  788.      int getdst;
  789. {
  790.   unsigned int *i = p->arg_info;
  791.   int had_src = 0;
  792.  
  793.   allregs = 1;
  794.   while (*i)
  795.     {
  796.  
  797.       current_name = reg_n (*i);
  798.       current_size = size_name (p->type);
  799.       switch (*i & CLASS_MASK)
  800.     {
  801.     case CLASS_X:
  802.     case CLASS_IR:
  803.     case CLASS_BA:
  804.     case CLASS_BX:
  805.     case CLASS_DA:
  806.       if (!getdst && IS_DST (*i))
  807.         break;
  808.       emit ("register int op_<name>= get_<size>_<mem>_da(context,oplval_<name>);\n");
  809.       allregs = 0;
  810.       break;
  811.     case CLASS_IMM:
  812.       if (!had_src)
  813.         {
  814.           if (p->opcode == OPC_out ||
  815.           p->opcode == OPC_outb ||
  816.           p->opcode == OPC_sout ||
  817.           p->opcode == OPC_soutb)
  818.         {
  819.           /* The imm is a dest here */
  820.           emit ("register int op_dst = imm_src;\n");
  821.         }
  822.           else
  823.         {
  824.           emit ("register int op_src = imm_src;\n");
  825.         }
  826.         }
  827.       break;
  828.     case CLASS_REG_QUAD:
  829.       if (!getdst && IS_DST (*i))
  830.         break;
  831.       had_src |= IS_SRC (*i);
  832.       emit ("UDItype op_<name> ;\n");
  833.  
  834.       break;
  835.     case CLASS_REG_WORD:
  836.       if (!getdst && IS_DST (*i))
  837.         break;
  838.       had_src |= IS_SRC (*i);
  839.       emit ("register int op_<name> = get_word_reg(context,reg_<name>);\n");
  840.       break;
  841.  
  842.     case CLASS_REG_LONG:
  843.       if (!getdst && IS_DST (*i))
  844.         break;
  845.       had_src |= IS_SRC (*i);
  846.       emit ("register int op_<name> = get_long_reg(context,reg_<name>);\n");
  847.       break;
  848.     case CLASS_REG_BYTE:
  849.       if (!getdst && IS_DST (*i))
  850.         break;
  851.       had_src |= IS_SRC (*i);
  852.       emit ("register int op_<name> = get_byte_reg(context,reg_<name>);\n");
  853.       break;
  854.     }
  855.       i++;
  856.     }
  857. }
  858.  
  859. static void
  860. normal_flags (p, s)
  861.      opcode_entry_type *p;
  862.      char *s;
  863. {
  864.   emit (" %s;\n", s);
  865.   emit ("NORMAL_FLAGS(context,%d, tmp,  op_dst, op_src); \n", p->type);
  866. }
  867.  
  868. static void
  869. test_normal_flags (p, s, opt)
  870.      opcode_entry_type *p;
  871.      char *s;
  872.      int opt;
  873. {
  874.   emit (" %s;\n", s);
  875.   if (0 && opt)
  876.     {
  877.       emit ("context->broken_flags = TST_FLAGS;\n");
  878.       emit ("context->size = %d;\n", p->type);
  879.     }
  880.   else
  881.     {
  882.       emit ("TEST_NORMAL_FLAGS(context,%d, tmp); \n", p->type);
  883.     }
  884.  
  885. }
  886.  
  887. static void
  888. optimize_normal_flags (p, s)
  889.      opcode_entry_type *p;
  890.      char *s;
  891. {
  892.   emit (" %s;\n", s);
  893. #if 0
  894.   emit ("context->broken_flags = CMP_FLAGS;\n");
  895. #else
  896.   emit ("NORMAL_FLAGS(context,%d, tmp,  op_dst, op_src); \n", p->type);
  897. #endif
  898. }
  899.  
  900. static
  901. void
  902. jp (p)
  903.      opcode_entry_type *p;
  904. {
  905.  
  906.   emit ("if(op_cc == 8 || COND(context,op_cc)) pc = oplval_dst;\n");
  907. }
  908.  
  909. static void
  910. jr (p)
  911.      opcode_entry_type *p;
  912. {
  913.   emit ("if(op_cc == 8 || COND(context,op_cc)) pc = oplval_dst;\n");
  914. }
  915.  
  916. static void
  917. ret (p)
  918.      opcode_entry_type *p;
  919. {
  920.   emit ("if(op_cc == 8 || COND(context,op_cc))\n{\n");
  921.   emit ("pc = get_<ptr_mode>_<mem>_ir(context,<sp>);\n");
  922.   emit ("put_<ptr_mode>_reg(context,<sp>, get_<ptr_mode>_reg(context,<sp>) + <ptr_size>);\n");
  923.   emit ("};\n");
  924. }
  925.  
  926. static void
  927. call (p)
  928.      opcode_entry_type *p;
  929. {
  930.   emit ("put_<ptr_mode>_reg(context,<sp>,tmp =  get_<ptr_mode>_reg(context,<sp>) - <ptr_size>);\n");
  931.   emit ("put_<ptr_mode>_<mem>_da(context,tmp, pc);\n");
  932.   emit ("pc = oplval_dst;\n");
  933. }
  934.  
  935. static void
  936. push (p)
  937.      opcode_entry_type *p;
  938. {
  939.   emit ("tmp = op_src;\n");
  940.   emit ("oplval_dst -= %d;\n", p->type / 8);
  941.   emit ("put_<ptr_mode>_reg(context,reg_dst, oplval_dst);\n");
  942. }
  943.  
  944. static void
  945. pop (p)
  946.      opcode_entry_type *p;
  947. {
  948.   emit ("tmp = op_src;\n");
  949.   emit ("put_<ptr_mode>_reg(context,reg_src, oplval_src + %d);\n", p->type / 8);
  950. }
  951.  
  952. static void
  953. ld (p)
  954.      opcode_entry_type *p;
  955. {
  956.   emit ("tmp = op_src;\n");
  957. }
  958.  
  959. static void
  960. sc ()
  961. {
  962.   emit ("support_call(context,imm_src);\n");
  963. }
  964.  
  965. static void
  966. bpt ()
  967. {
  968.   emit ("pc -=2; \n");
  969.   emit ("context->exception = SIM_BREAKPOINT;\n");
  970. }
  971.  
  972. static void
  973. ldi (p, size, inc)
  974.      opcode_entry_type *p;
  975.      int size;
  976.      int inc;
  977. {
  978.   int dinc = (size / 8) * inc;
  979.  
  980.   current_size = size_name (size);
  981.   emit ("{ \n");
  982.   emit ("int type = %s;\n", insn_4 (7));
  983.   emit ("int rs = get_<ptr_mode>_reg(context,reg_src);\n");
  984.   emit ("int rd = get_<ptr_mode>_reg(context,reg_dst);\n");
  985.   emit ("int rr = get_word_reg(context,reg_aux_r);\n");
  986.   emit ("do {\n");
  987.   emit ("put_<size>_<mem>_da(context,rd, get_<size>_<mem>_da(context,rs));\n");
  988.   emit ("rd += %d;\n", dinc);
  989.   emit ("rs += %d;\n", dinc);
  990.   emit ("rr --;\n");
  991.   emit ("context->cycles += 9;\n");
  992.   emit ("} while (!type && rr != 0 && context->exception <= 1);\n");
  993.   emit ("if (context->exception>1) pc -=4;\n");
  994.   emit ("put_<ptr_mode>_reg(context,reg_src, rs);\n");
  995.   emit ("put_<ptr_mode>_reg(context,reg_dst, rd);\n");
  996.   emit ("put_word_reg(context,reg_aux_r, rr);\n");
  997.   emit ("}\n");
  998.  
  999. }
  1000.  
  1001. static void
  1002. shift (p, arith)
  1003.      opcode_entry_type *p;
  1004.      int arith;
  1005. {
  1006.   emit ("op_src = (char)op_src;\n");
  1007.   emit ("if (op_src < 0) \n");
  1008.   emit ("{\n");
  1009.   emit ("op_src = -op_src;\n");
  1010.   emit ("op_dst = (%s <c_size>)op_dst;\n", arith ? "" : "unsigned");
  1011.   emit ("tmp = (%s op_dst) >> op_src;\n", arith ? "" : "(unsigned)");
  1012.   emit ("context->carry = op_dst >> (op_src-1);\n", p->type);
  1013.   emit ("}\n");
  1014.   emit ("else\n");
  1015.   emit ("{\n");
  1016.   emit ("tmp = op_dst << op_src;\n");
  1017.   emit ("context->carry = op_dst >> (%d - op_src);\n", p->type);
  1018.   emit ("}\n");
  1019.   emit ("context->zero = tmp == 0;\n");
  1020.   emit ("context->sign = (int)tmp < 0;\n");
  1021.   emit ("context->overflow = ((int)tmp < 0) != ((int)op_dst < 0);\n");
  1022.   emit ("context->cycles += 3*op_src;\n");
  1023.   emit ("context->broken_flags = 0;\n");
  1024. }
  1025.  
  1026. static void
  1027. rotate (p, through_carry, size, left)
  1028.      opcode_entry_type *p;
  1029.      int through_carry;
  1030.      int size;
  1031.      int left;
  1032. {
  1033.   if (!left)
  1034.     {
  1035.       emit ("while (op_src--) {\n");
  1036.       emit ("int rotbit;\n");
  1037.       emit ("rotbit = tmp & 1;\n");
  1038.       emit ("tmp = ((unsigned)op_dst) >> op_src;\n");
  1039.  
  1040.       if (through_carry)
  1041.     {
  1042.       emit ("tmp |= context->carry << %d;\n", size - 1);
  1043.     }
  1044.       else
  1045.     {
  1046.       emit ("tmp |= rotbit << %d;\n", size - 1);
  1047.     }
  1048.       emit ("context->carry = rotbit;\n");
  1049.       emit ("}\n");
  1050.     }
  1051.   else
  1052.     {
  1053.       emit ("while (op_src--) {\n");
  1054.       emit ("int rotbit;\n");
  1055.  
  1056.       emit ("rotbit = (op_dst >> (%d))&1;\n", size - 1);
  1057.       emit ("op_dst <<=1;\n");
  1058.       if (through_carry)
  1059.     {
  1060.       emit ("if (context->carry) op_dst |=1;\n");
  1061.     }
  1062.       else
  1063.     {
  1064.       emit ("if (rotbit) op_dst |= 1;\n");
  1065.     }
  1066.       emit ("context->carry = rotbit;\n");
  1067.       emit ("}\n");
  1068.     }
  1069.   emit ("tmp = op_dst;\n");
  1070.   emit ("context->zero = tmp == 0;\n");
  1071.   emit ("context->sign = (int)tmp < 0;\n");
  1072.   emit ("context->overflow = ((int)tmp < 0) != ((int)op_dst < 0);\n");
  1073.   emit ("context->cycles += 3*op_src;\n");
  1074.   emit ("context->broken_flags = 0;\n");
  1075. }
  1076.  
  1077. static void
  1078. adiv (p)
  1079.      opcode_entry_type *p;
  1080. {
  1081.   emit ("if (op_src==0)\n");
  1082.   emit ("{\n");
  1083.   emit ("context->exception = SIM_DIV_ZERO;\n");
  1084.   emit ("}\n");
  1085.   emit ("else\n");
  1086.   emit ("{\n");
  1087.  
  1088.   if (p->type == 32)
  1089.     {
  1090.       emit ("op_dst.low = get_long_reg(context,reg_dst+2);\n");
  1091.       emit ("op_dst.high = get_long_reg(context,reg_dst+0);\n");
  1092.       emit ("tmp = op_dst.low / op_src;\n");
  1093.       emit ("put_long_reg(context,reg_dst+2, tmp);\n");
  1094.       emit ("put_long_reg(context,reg_dst, op_dst.low %% op_src);\n");
  1095.       emit ("context->zero = op_src == 0 || (op_dst.low==0 && op_dst.high==0);\n");
  1096.     }
  1097.   else
  1098.     {
  1099.       emit ("tmp = op_dst / op_src;\n");
  1100.       emit ("put_word_reg(context,reg_dst+1, tmp);\n");
  1101.       emit ("put_word_reg(context,reg_dst,  op_dst %% op_src);\n");
  1102.       emit ("context->zero = op_src == 0 || op_dst==0;\n");
  1103.     }
  1104.  
  1105.   emit ("context->sign = (int)tmp < 0;\n");
  1106.   emit ("context->overflow =(tmp & 0x%x) != 0;\n",
  1107.     ~((1 << (p->type)) - 1));
  1108.   emit ("context->carry = (tmp & 0x%x) != 0;\n",
  1109.     ~(1 << (p->type)));
  1110.  
  1111.   emit ("}\n");
  1112. }
  1113.  
  1114. static void
  1115. dobit (p)
  1116. opcode_entry_type *p;
  1117. {
  1118.   emit("context->zero = (op_dst & (1<<op_src))==0;\n");
  1119.   emit("context->broken_flags = 0;\n");
  1120. }
  1121. static void
  1122. doset (p, v)
  1123. opcode_entry_type*p;
  1124. int v;
  1125. {
  1126.   if (v) 
  1127.     emit (" tmp = op_dst | (1<< op_src);\n");
  1128.   else
  1129.     emit (" tmp = op_dst & ~(1<< op_src);\n");
  1130. }
  1131.  
  1132. static void
  1133. mult (p)
  1134.      opcode_entry_type *p;
  1135. {
  1136.  
  1137.   if (p->type == 32)
  1138.     {
  1139.       emit ("op_dst.low =  get_long_reg(context,reg_dst+2);\n");
  1140.       emit ("tmp = op_dst.low * op_src;\n");
  1141.       emit ("put_long_reg(context,reg_dst+2, tmp);\n");
  1142.       emit ("put_long_reg(context,reg_dst, 0);\n");
  1143.     }
  1144.   else
  1145.     {
  1146.       emit ("op_dst =  get_word_reg(context,reg_dst+1);\n");
  1147.       emit ("tmp = op_dst * op_src;\n");
  1148.       emit ("put_long_reg(context,reg_dst, tmp);\n");
  1149.     }
  1150.  
  1151.   emit ("context->sign = (int)tmp < 0;\n");
  1152.   emit ("context->overflow =0;\n");
  1153.   emit ("context->carry = (tmp & 0x%x) != 0;\n", ~((1 << (p->type)) - 1));
  1154.   emit ("context->zero = tmp == 0;\n");
  1155.  
  1156. }
  1157.  
  1158. static void
  1159. exts (p)
  1160.      opcode_entry_type *p;
  1161. {
  1162.   /* Fetch the ls part of the src */
  1163.   current_size = size_name (p->type * 2);
  1164.  
  1165.   if (p->type == 32)
  1166.     {
  1167.       emit ("tmp= get_long_reg(context,reg_dst+2);\n");
  1168.       emit ("if (tmp & (1<<%d)) {\n", p->type - 1);
  1169.       emit ("put_long_reg(context,reg_dst, 0xffffffff);\n");
  1170.       emit ("}\n");
  1171.       emit ("else\n");
  1172.       emit ("{\n");
  1173.       emit ("put_long_reg(context,reg_dst, 0);\n");
  1174.       emit ("}\n");
  1175.     }
  1176.   else
  1177.     {
  1178.       emit ("tmp= get_<size>_reg(context,reg_dst);\n");
  1179.       emit ("if (tmp & (1<<%d)) {\n", p->type - 1);
  1180.       emit ("tmp |= 0x%x;\n", ~((1 << p->type) - 1));
  1181.       emit ("}\n");
  1182.       emit ("else\n");
  1183.       emit ("{\n");
  1184.  
  1185.       emit ("tmp &= 0x%x;\n", ((1 << p->type) - 1));
  1186.       emit ("}\n");
  1187.       emit ("put_<size>_reg(context,reg_dst, tmp);\n");
  1188.     }
  1189. }
  1190.  
  1191. /* emit code to perform operation */
  1192. void
  1193. info_docode (p)
  1194.      opcode_entry_type *p;
  1195. {
  1196.   switch (p->opcode)
  1197.     {
  1198.     case OPC_clr:
  1199.     case OPC_clrb:
  1200.       emit ("tmp = 0;\n");
  1201.       break;
  1202.     case OPC_ex:
  1203.     case OPC_exb:
  1204.  
  1205.       emit ("tmp = op_src; \n");
  1206.       if (allregs)
  1207.     {
  1208.       emit ("put_<size>_reg(context,reg_src, op_dst);\n");
  1209.     }
  1210.       else
  1211.     {
  1212.       emit ("put_<size>_mem_da(context, oplval_src, op_dst);\n");
  1213.     }
  1214.       break;
  1215.     case OPC_adc:
  1216.     case OPC_adcb:
  1217.       optimize_normal_flags (p, "tmp = op_dst + op_src + PSW_CARRY");
  1218.       break;
  1219.     case OPC_nop:
  1220.       break;
  1221.     case OPC_com:
  1222.     case OPC_comb:
  1223.       test_normal_flags (p, "tmp = ~ op_dst", 1);
  1224.       break;
  1225.     case OPC_and:
  1226.     case OPC_andb:
  1227.       test_normal_flags (p, "tmp = op_dst & op_src", 1);
  1228.       break;
  1229.     case OPC_xor:
  1230.     case OPC_xorb:
  1231.       test_normal_flags (p, "tmp = op_dst ^ op_src", 1);
  1232.       break;
  1233.     case OPC_or:
  1234.     case OPC_orb:
  1235.       test_normal_flags (p, "tmp = op_dst | op_src", 1);
  1236.       break;
  1237.     case OPC_sla:
  1238.     case OPC_slab:
  1239.     case OPC_slal:
  1240.     case OPC_sda:
  1241.     case OPC_sdab:
  1242.     case OPC_sdal:
  1243.       shift (p, 1);
  1244.       break;
  1245.  
  1246.     case OPC_sll:
  1247.     case OPC_sllb:
  1248.     case OPC_slll:
  1249.     case OPC_sdl:
  1250.     case OPC_sdlb:
  1251.     case OPC_sdll:
  1252.       shift (p, 0);
  1253.       break;
  1254.     case OPC_rl:
  1255.       rotate (p, 0, 16, 1);
  1256.       break;
  1257.     case OPC_rlb:
  1258.       rotate (p, 0, 8, 1);
  1259.       break;
  1260.     case OPC_rr:
  1261.       rotate (p, 0, 16, 0);
  1262.       break;
  1263.     case OPC_rrb:
  1264.       rotate (p, 0, 8, 0);
  1265.       break;
  1266.     case OPC_rrc:
  1267.       rotate (p, 1, 16, 0);
  1268.     case OPC_rrcb:
  1269.       rotate (p, 1, 8, 0);
  1270.       break;
  1271.     case OPC_rlc:
  1272.       rotate (p, 1, 16, 1);
  1273.       break;
  1274.     case OPC_rlcb:
  1275.       rotate (p, 1, 8, 1);
  1276.       break;
  1277.  
  1278.     case OPC_extsb:
  1279.     case OPC_exts:
  1280.     case OPC_extsl:
  1281.       exts (p);
  1282.       break;
  1283.     case OPC_add:
  1284.     case OPC_addb:
  1285.     case OPC_addl:
  1286.     case OPC_inc:
  1287.     case OPC_incb:
  1288.       optimize_normal_flags (p, "tmp = op_dst + op_src");
  1289.       break;
  1290.     case OPC_testb:
  1291.     case OPC_test:
  1292.     case OPC_testl:
  1293.       test_normal_flags (p, "tmp = op_dst", 0);
  1294.       break;
  1295.     case OPC_cp:
  1296.     case OPC_cpb:
  1297.     case OPC_cpl:
  1298.       normal_flags (p, "op_src = -op_src; tmp = op_dst + op_src");
  1299.       break;
  1300.     case OPC_negb:
  1301.     case OPC_neg:
  1302.       emit ("{\n");
  1303.       emit ("int op_src = -op_dst;\n");
  1304.       emit ("op_dst = 0;\n");
  1305.       optimize_normal_flags (p, "tmp = op_dst + op_src;\n");
  1306.       emit ("}");
  1307.       break;
  1308.  
  1309.     case OPC_sub:
  1310.     case OPC_subb:
  1311.     case OPC_subl:
  1312.     case OPC_dec:
  1313.     case OPC_decb:
  1314.       optimize_normal_flags (p, "op_src = -op_src ;tmp = op_dst + op_src");
  1315.       break;
  1316.     case OPC_bpt:
  1317.       bpt ();
  1318.       break;
  1319.     case OPC_jr:
  1320.       jr (p);
  1321.       break;
  1322.     case OPC_sc:
  1323.       sc ();
  1324.       break;
  1325.     case OPC_jp:
  1326.       jp (p);
  1327.       break;
  1328.     case OPC_ret:
  1329.       ret (p);
  1330.       break;
  1331.     case OPC_call:
  1332.       call (p);
  1333.       break;
  1334.     case OPC_tcc:
  1335.     case OPC_tccb:
  1336.       emit ("if(op_cc == 8 || COND(context,op_cc)) put_word_reg(context,reg_dst, 1);\n");
  1337.       break;
  1338.     case OPC_lda:
  1339.       emit ("tmp = oplval_src; \n");
  1340.       /*(((oplval_src) & 0xff0000) << 8) | (oplval_src & 0xffff); \n");*/
  1341.       break;
  1342.     case OPC_ldk:
  1343.     case OPC_ld:
  1344.  
  1345.     case OPC_ldb:
  1346.     case OPC_ldl:
  1347.       ld (p);
  1348.       break;
  1349.     case OPC_ldib:
  1350.       ldi (p, 8, 1);
  1351.       break;
  1352.     case OPC_ldi:
  1353.       ldi (p, 16, 1);
  1354.       break;
  1355.  
  1356.     case OPC_lddb:
  1357.       ldi (p, 8, -1);
  1358.       break;
  1359.     case OPC_ldd:
  1360.       ldi (p, 16, -1);
  1361.       break;
  1362.  
  1363.     case OPC_push:
  1364.     case OPC_pushl:
  1365.       push (p);
  1366.       break;
  1367.  
  1368.     case OPC_div:
  1369.     case OPC_divl:
  1370.       adiv (p);
  1371.       break;
  1372.     case OPC_mult:
  1373.     case OPC_multl:
  1374.       mult (p);
  1375.       break;
  1376.     case OPC_pop:
  1377.     case OPC_popl:
  1378.       pop (p);
  1379.       break;
  1380.     case OPC_set:
  1381.       doset (p,1);
  1382.       break;
  1383.     case OPC_res:
  1384.       doset (p,0);
  1385.       break;
  1386.     case OPC_bit:
  1387.       dobit(p);
  1388.       break;
  1389.     default:
  1390.  
  1391.       emit ("tmp = fail(context,%d);\n", p->opcode);
  1392.       break;
  1393.     }
  1394. }
  1395.  
  1396. /* emit code to store result in calculated lvalue */
  1397.  
  1398. void
  1399. info_store (p)
  1400.      opcode_entry_type *p;
  1401. {
  1402.   unsigned int *i = p->arg_info;
  1403.  
  1404.   while (*i)
  1405.     {
  1406.       current_name = reg_n (*i);
  1407.       current_size = size_name (p->type);
  1408.  
  1409.       if (IS_DST (*i))
  1410.     {
  1411.       switch (*i & CLASS_MASK)
  1412.         {
  1413.         case CLASS_PR:
  1414.           emit ("put_<ptr_mode>_reg(context,reg_<name>, tmp);\n");
  1415.           break;
  1416.         case CLASS_REG_LONG:
  1417.         case CLASS_REG_WORD:
  1418.         case CLASS_REG_BYTE:
  1419.  
  1420.           emit ("put_<size>_reg(context,reg_<name>,tmp);\n");
  1421.           break;
  1422.         case CLASS_X:
  1423.         case CLASS_IR:
  1424.         case CLASS_DA:
  1425.         case CLASS_BX:
  1426.         case CLASS_BA:
  1427.  
  1428.           emit ("put_<size>_<mem>_da(context,oplval_<name>, tmp);\n");
  1429.           break;
  1430.         case CLASS_IMM:
  1431.           break;
  1432.         default:
  1433.           emit ("abort(); ");
  1434.           break;
  1435.         }
  1436.  
  1437.     }
  1438.       i++;
  1439.     }
  1440. }
  1441.  
  1442. static
  1443. void
  1444. mangle (p, shortcut, value)
  1445.      opcode_entry_type *p;
  1446.      int shortcut;
  1447.      int value;
  1448. {
  1449.   int nostore = 0;
  1450.   int extra;
  1451.   int getdst = 1;
  1452.   int before = 0;
  1453.   int nosrc = 0;
  1454.  
  1455.   emit ("/\052 %s \052/\n", p->nicename);
  1456.   if (shortcut)
  1457.     {
  1458.       emit ("int <fop>_%04x(context,pc)\n", value);
  1459.     }
  1460.   else
  1461.     {
  1462.       emit ("int <fop>_%d(context,pc,iwords0)\n", p->idx);
  1463.       emit ("int iwords0;\n");
  1464.     }
  1465.   emit ("sim_state_type *context;\n");
  1466.   emit ("int pc;\n");
  1467.   emit ("{\n");
  1468.   emit ("register unsigned int tmp;\n");
  1469.   if (shortcut)
  1470.     {
  1471.       emit ("register unsigned int iwords0 = 0x%x;\n", value);
  1472.     }
  1473.  
  1474.   /* work out how much bigger this opcode could be because it's large
  1475.      model */
  1476.   if (BIG)
  1477.     {
  1478.       int i;
  1479.  
  1480.       extra = 0;
  1481.       for (i = 0; i < 4; i++)
  1482.     {
  1483.       if ((p->arg_info[i] & CLASS_MASK) == CLASS_DA
  1484.           || (p->arg_info[i] & CLASS_MASK) == CLASS_X)
  1485.         extra += 2;
  1486.     }
  1487.     }
  1488.   else
  1489.     {
  1490.       extra = 0;
  1491.     }
  1492.   printf ("            /* Length %d */ \n", p->length + extra);
  1493.   switch (p->length + extra)
  1494.     {
  1495.     case 2:
  1496.       emit ("pc += 2\n;");
  1497.       break;
  1498.     case 4:
  1499.       emit ("register unsigned int iwords1 = get_word_mem_da(context,pc+2);\n");
  1500.       emit ("pc += 4;\n");
  1501.       break;
  1502.     case 6:
  1503.  
  1504.       emit ("register unsigned int iwords1 = get_word_mem_da(context,pc+2);\n");
  1505.       emit ("register unsigned int iwords2 = get_word_mem_da(context,pc+4);\n");
  1506.       emit ("pc += 6;\n");
  1507.       break;
  1508.     case 8:
  1509.       emit ("register unsigned int iwords1 = get_word_mem_da(context,pc+2);\n");
  1510.       emit ("register unsigned int iwords2 = get_word_mem_da(context,pc+4);\n");
  1511.       emit ("register unsigned int iwords3 = get_word_mem_da(context,pc+6);\n");
  1512.       emit ("pc += 8;\n");
  1513.       break;
  1514.     default:
  1515.       break;
  1516.  
  1517.     }
  1518.   emit ("context->cycles += %d;\n", p->cycles);
  1519.  
  1520.   emit ("{\n");
  1521.   info_args (p);
  1522.   info_special (p, &getdst, &nostore, &before, &nosrc);
  1523.  
  1524.   info_lvals (p);
  1525.   if (!nosrc)
  1526.     {
  1527.       info_fetch (p, getdst);
  1528.     }
  1529.  
  1530.   if (before)
  1531.     {
  1532.       info_docode (p);
  1533.     }
  1534.   else
  1535.     {
  1536.       info_docode (p);
  1537.     }
  1538.   if (!nostore)
  1539.     info_store (p);
  1540.   emit ("}\n");
  1541.   emit ("return pc;\n");
  1542.   emit ("}\n");
  1543. }
  1544.  
  1545. void
  1546. static
  1547. one_instruction (i)
  1548.      int i;
  1549. {
  1550.   /* find the table entry */
  1551.   opcode_entry_type *p = z8k_table + i;
  1552.  
  1553.   if (!p)
  1554.     return;
  1555.   mangle (p, 0, 0);
  1556. }
  1557.  
  1558. void
  1559. add_to_list (ptr, value)
  1560.      struct opcode_value **ptr;
  1561.      int value;
  1562. {
  1563.   struct opcode_value *prev;
  1564.  
  1565.   prev = *ptr;
  1566.   *ptr = (struct opcode_value *) malloc (sizeof (struct opcode_value));
  1567.  
  1568.   (*ptr)->n = value;
  1569.   (*ptr)->next = prev;
  1570. }
  1571.  
  1572. void
  1573. build_list (i)
  1574.      int i;
  1575. {
  1576.   opcode_entry_type *p = lookup_inst (i);
  1577.  
  1578.   if (!p)
  1579.     return;
  1580.   add_to_list (&list[p->idx], i);
  1581. }
  1582.  
  1583. int
  1584. main (ac, av)
  1585.      int ac;
  1586.      char **av;
  1587. {
  1588.   int i;
  1589.   int needcomma = 0;
  1590.  
  1591.   makelist = 0;
  1592.  
  1593.   for (i = 1; i < ac; i++)
  1594.     {
  1595.       if (strcmp (av[i], "-m") == 0)
  1596.     makelist = 1;
  1597.       if (strcmp (av[i], "-1") == 0)
  1598.     file = 1;
  1599.       if (strcmp (av[i], "-2") == 0)
  1600.     file = 2;
  1601.       if (strcmp (av[i], "-3") == 0)
  1602.     file = 3;
  1603.       if (strcmp (av[i], "-b3") == 0)
  1604.     {
  1605.       file = 3;
  1606.       BIG = 1;
  1607.     }
  1608.  
  1609.     }
  1610.   if (makelist)
  1611.     {
  1612.  
  1613.       int i;
  1614.       needcomma = 0;
  1615.       printf ("short int z8k_inv_list[] = {\n");
  1616.  
  1617.       for (i = 0; i < 1 << 16; i++)
  1618.     {
  1619.       opcode_entry_type *p = lookup_inst (i);
  1620.  
  1621.       if(needcomma)
  1622.         printf(",");
  1623.       if ((i & 0xf) == 0)
  1624.         printf ("\n");
  1625.  
  1626. #if 0
  1627.       printf ("\n        /*%04x %s */", i, p ? p->nicename : "");
  1628. #endif
  1629.  
  1630.       if (!p)
  1631.         {
  1632.           printf ("-1");
  1633.         }
  1634.       else
  1635.         {
  1636.           printf ("%d", p->idx);
  1637.         }
  1638.       if ((i & 0x3f) == 0)
  1639.         {
  1640.           printf ("\n#ifdef __GNUC__\n");
  1641.           printf ("};\n");
  1642.           printf("short int int_list%d[] = {\n", i);
  1643.           printf ("#else\n");
  1644.           printf (",\n");
  1645.           printf ("#endif\n");
  1646.           needcomma = 0;
  1647.         }
  1648.       else
  1649.         needcomma = 1;
  1650.  
  1651.     }
  1652.       printf ("};\n");
  1653.       return 1;
  1654.     }
  1655.  
  1656.   /* First work out which opcodes use which bit patterns,
  1657.      build a list of all matching bit pattens */
  1658.   for (i = 0; i < 1 << 16; i++)
  1659.     {
  1660.       build_list (i);
  1661.     }
  1662. #if DUMP_LIST
  1663.   for (i = 0; i < NOPS; i++)
  1664.     {
  1665.       struct opcode_value *p;
  1666.  
  1667.       printf ("%d,", i);
  1668.       p = list[i];
  1669.       while (p)
  1670.     {
  1671.       printf (" %04x,", p->n);
  1672.       p = p->next;
  1673.     }
  1674.       printf ("-1\n");
  1675.     }
  1676.  
  1677. #endif
  1678.  
  1679.   if (file == 1)
  1680.     {
  1681.       extern int quick[];
  1682.  
  1683.       /* Do the shortcuts */
  1684.       printf ("            /* SHORTCUTS */\n");
  1685.       for (i = 0; quick[i]; i++)
  1686.     {
  1687.       int t = quick[i];
  1688.  
  1689.       mangle (z8k_table + z8k_inv_list[t],
  1690.           1,
  1691.           t);
  1692.     }
  1693.     }
  1694.   if (file == 3)
  1695.     {
  1696.       printf ("            /* NOT -SHORTCUTS */\n");
  1697.       for (i = 0; i < NOPS; i++)
  1698.     {
  1699.       if (list[i])
  1700.         {
  1701.           one_instruction (i);
  1702.         }
  1703.       else
  1704.         {
  1705.           emit ("int <fop>_%d(context,pc)\n", i);
  1706.           printf ("sim_state_type *context;\n");
  1707.           printf ("int pc;\n");
  1708.           emit ("{ <fop>_bad1();return pc; }\n");
  1709.         }
  1710.     }
  1711.       emit ("int <fop>_bad() ;\n");
  1712.  
  1713.       /* Write the jump table */
  1714.       emit ("int (*(<fop>_table[]))() = {");
  1715.       needcomma = 0;
  1716.       for (i = 0; i < NOPS; i++)
  1717.     {
  1718.       if (needcomma)
  1719.         printf (",");
  1720.       emit ("<fop>_%d\n", i);
  1721.       needcomma = 1;
  1722.       if ((i & 0x3f) == 0)
  1723.         {
  1724.           printf ("#ifdef __GNUC__\n");
  1725.           printf ("};\n");
  1726.           emit ("int (*(<fop>_table%d[]))() = {\n", i);
  1727.           printf ("#else\n");
  1728.           printf (",\n");
  1729.           printf ("#endif\n");
  1730.           needcomma = 0;
  1731.         }
  1732.     }
  1733.       emit ("};\n");
  1734.     }
  1735.  
  1736.   if (file == 2)
  1737.     {
  1738.       extern int quick[];
  1739.       /* Static - since it's too be to be automatic on the apollo */
  1740.       static int big[64 * 1024];
  1741.  
  1742.       for (i = 0; i < 64 * 1024; i++)
  1743.     big[i] = 0;
  1744.  
  1745.       for (i = 0; quick[i]; i++)
  1746.     {
  1747. #if 0
  1748.  
  1749.       printf ("extern int <fop>_%04x();\n", quick[i]);
  1750. #endif
  1751.  
  1752.       big[quick[i]] = 1;
  1753.     }
  1754.  
  1755.       for (i = 0; i < NOPS; i++)
  1756.     {
  1757. #if 0
  1758.       printf ("extern int fop_%d();\n", i);
  1759. #endif
  1760.     }
  1761. #if 0
  1762.       printf ("extern int fop_bad();\n");
  1763. #endif
  1764.       printf ("struct op_info op_info_table[] = {\n");
  1765.       for (i = 0; i < 1 << 16; i++)
  1766.     {
  1767.       int inv = z8k_inv_list[i];
  1768.       opcode_entry_type *p = z8k_table + inv;
  1769.  
  1770.       if (needcomma)
  1771.         printf (",");
  1772. #if 0
  1773.       if (big[i])
  1774.         {
  1775.           printf ("<fop>_%04x", i);
  1776.         }
  1777.       else
  1778. #endif
  1779.       if (inv >= 0)
  1780.         {
  1781.           printf ("%d", inv);
  1782.         }
  1783.       else
  1784.         printf ("400");
  1785.       if (inv >= 0)
  1786.         {
  1787.           printf ("        /* %04x %s */\n", i, p->nicename);
  1788.         }
  1789.       else
  1790.         {
  1791.           printf ("\n");
  1792.         }
  1793.       needcomma = 1;
  1794.       if ((i & 0x3f) == 0)
  1795.         {
  1796.           printf ("#ifdef __GNUC__\n");
  1797.           printf ("}; \n");
  1798.           printf ("struct op_info op_info_table%d[] = {\n", i);
  1799.           printf ("#else\n");
  1800.           printf (",\n");
  1801.  
  1802.           printf ("#endif\n");
  1803.           needcomma = 0;
  1804.         }
  1805.     }
  1806.       printf ("};\n");
  1807.  
  1808.     }
  1809.   return 0;
  1810. }
  1811.  
  1812. char *
  1813. insn_ptr (n)
  1814.      int n;
  1815. {
  1816.   if (BIG)
  1817.     {
  1818.       abort ();
  1819.     }
  1820.  
  1821.   switch (n)
  1822.     {
  1823.     case 4:
  1824.       return "iwords_1";
  1825.     default:
  1826.       return "fail(context,123)";
  1827.     }
  1828. }
  1829.  
  1830. /* work out if the opcode only wants lvalues */
  1831. int
  1832. lvalue (p)
  1833.      opcode_entry_type *p;
  1834. {
  1835.   switch (p->opcode)
  1836.     {
  1837.     case OPC_lda:
  1838.       return 1;
  1839.     case OPC_call:
  1840.     case OPC_jp:
  1841.       return 1;
  1842.     default:
  1843.       return 0;
  1844.     }
  1845. }
  1846.  
  1847. int
  1848. info_len_in_words (o)
  1849.      opcode_entry_type *o;
  1850. {
  1851.   unsigned  int *p = o->byte_info;
  1852.   int nibs = 0;
  1853.  
  1854.   while (*p)
  1855.     {
  1856.       switch (*p & CLASS_MASK)
  1857.     {
  1858.     case CLASS_BIT:
  1859.     case CLASS_REGN0:
  1860.     case CLASS_REG:
  1861.     case CLASS_01II:
  1862.     case CLASS_00II:
  1863.       nibs++;
  1864.       break;
  1865.     case CLASS_ADDRESS:
  1866.       nibs += SIZE_ADDRESS;
  1867.       break;
  1868.     case CLASS_IMM:
  1869.       switch (*p & ~CLASS_MASK)
  1870.         {
  1871.         case ARG_IMM16:
  1872.           nibs += 4;
  1873.           break;
  1874.         case ARG_IMM32:
  1875.           nibs += 8;
  1876.           break;
  1877.         case ARG_IMM2:
  1878.         case ARG_IMM4:
  1879.         case ARG_IMM4M1:
  1880.         case ARG_IMM_1:
  1881.         case ARG_IMM_2:
  1882.         case ARG_IMMNMINUS1:
  1883.           nibs++;
  1884.           break;
  1885.         case ARG_NIM8:
  1886.  
  1887.         case ARG_IMM8:
  1888.           nibs += 2;
  1889.           break;
  1890.         default:
  1891.           abort ();
  1892.         }
  1893.       break;
  1894.     case CLASS_DISP:
  1895.       switch (*p & ~CLASS_MASK)
  1896.         {
  1897.         case ARG_DISP16:
  1898.           nibs += 4;
  1899.           break;
  1900.         case ARG_DISP12:
  1901.           nibs += 3;
  1902.           break;
  1903.         case ARG_DISP8:
  1904.           nibs += 2;
  1905.           break;
  1906.         default:
  1907.           abort ();
  1908.         }
  1909.       break;
  1910.     case CLASS_0DISP7:
  1911.     case CLASS_1DISP7:
  1912.     case CLASS_DISP8:
  1913.       nibs += 2;
  1914.       break;
  1915.     case CLASS_BIT_1OR2:
  1916.     case CLASS_0CCC:
  1917.     case CLASS_1CCC:
  1918.     case CLASS_CC:
  1919.       nibs++;
  1920.       break;
  1921.     default:
  1922.       emit ("don't know %x\n", *p);
  1923.     }
  1924.       p++;
  1925.     }
  1926.  
  1927.   return nibs / 4;        /* return umber of words */
  1928. }
  1929.